import { Component, OnInit } from '@angular/core';
import { Router, NavigationExtras, ActivatedRoute } from '@angular/router';
import { Validators, FormBuilder, ValidationErrors, AbstractControl, FormGroup, FormControl } from '@angular/forms';
import {TranslateService} from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';
import { CommonResponse,VerificationType,ToastColor,CommonService,AppVerificationCode,AppParam } from '@services/common-service.service';
import {HttpClient} from "@angular/common/http";
import { AuthTokenService } from '@services/auth-token.service';
import { environment } from 'src/environments/environment';
@Component({
  selector: 'app-account-sms',
  templateUrl: './account-sms.page.html',
  styleUrls: ['./account-sms.page.scss'],
})
export class AccountSmsPage implements OnInit {
  public seconds:number = 10;

  public sendView:boolean = true;

  public defaultSmsCountDown:number;

  public smsCountDown:number;
  constructor(
    private router:Router,
    private translate: TranslateService,
    private route: ActivatedRoute,
    private authTokenService:AuthTokenService,
    private http:HttpClient,
    private fb: FormBuilder,
    private commonService:CommonService,
    private defaultLanguageService:DefaultLanguageService
  ) {
    this.accountForm = this.fb.group({
      accountSms: new FormControl('',{
        validators:Validators.required
      })
    });

    this.defaultSmsCountDown = parseInt(this.commonService.getAppParamFromCache(AppParam.SMS_COUNT_DOWN));
    this.smsCountDown = this.defaultSmsCountDown; 

    this.sendAppVerificationCode();
  }

  sendAppVerificationCode(){
    if(!this.authTokenService.getAppUserId()){return false;}
    let sendAppVerificationCode:AppVerificationCode = {
      appUserId:this.authTokenService.getAppUserId(),
      mobileNumber:'',
      verificationType:VerificationType.BANK_ACCOUNT,
      verificationCode:'',
      appCode:environment.APP_CODE
    }
    this.commonService.sendAppVerificationCode(sendAppVerificationCode)
    .subscribe((response:CommonResponse)=>{
      this.smsCountDown = this.defaultSmsCountDown;
      this.sendView = true;
      var id = setInterval(() => {
        this.smsCountDown--;
        if(this.smsCountDown == 0){
          clearInterval(id);
          this.sendView = false;
        }
      }, 1000);
    },(errorResponse)=>{
      this.commonService.commonToast(ToastColor.warning,errorResponse.error.errorMsg);
    })
  }

  accountForm! : FormGroup;

  ngOnInit() {
    // var id = setInterval(() => {
    //   this.seconds--;
    //   if(this.seconds == 0){
    //     clearInterval(id);
    //     this.sendView = true;
    //   }
    // }, 1000);
  }

  toNext(){
    let checkAppVerificationCode:AppVerificationCode = {
      appUserId:this.authTokenService.getAppUserId(),
      verificationCode:this.accountForm.value.accountSms,
      verificationType:VerificationType.BANK_ACCOUNT,
      mobileNumber:'',
      appCode:environment.APP_CODE
    }
    let navigationExtras: NavigationExtras = {
      queryParams: {}
    };
    this.commonService.checkAppVerificationCode(checkAppVerificationCode).subscribe((response:CommonResponse)=>{
      if(response.isSuccess && response.responseBody.isPass){
        this.router.navigate(['/tabs/myAccount/bank-account/add-bank-account/account-success'], navigationExtras);
      }else{
        this.commonService.commonToast(ToastColor.warning);
      }
    },(error)=>{
      this.commonService.commonToast(ToastColor.warning);
    });
   
   
  }
}
